package com.jtny.nytb.equipment.utils;


import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

@Component
public class RandomCodeGeneratorUtil {
    private Set<String> generatedCodes;

    public RandomCodeGeneratorUtil() {
        generatedCodes = new HashSet<>();
    }

    public String generateUniqueFourDigitCode() {
        Random random = new Random();
        StringBuilder codeBuilder = new StringBuilder();

        while (true) {
            codeBuilder.setLength(0);
            for (int i = 0; i < 4; i++) {
                int digit = random.nextInt(10);
                codeBuilder.append(digit);
            }

            String code = codeBuilder.toString();

            if (!generatedCodes.contains(code)) {
                generatedCodes.add(code);
                return code;
            }
        }
    }
}
